package de.sendorian.util.matcher;


import org.apache.commons.lang.ObjectUtils;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

@SuppressWarnings("unchecked")
public class BlacklistMatcher<T> extends TypeSafeDiagnosingMatcher<T> {

	private final Iterable unwantedObjects;


	public BlacklistMatcher(Iterable unwandetObjects) {
		this.unwantedObjects = unwandetObjects;
	}


	protected boolean matchesSafely(T item, Description mismatchDescription) {
		for (Object unwantedObject : unwantedObjects) {
			if (matchesInternal(item, unwantedObject)) {
				return true;
			}
		}
		return false;
	}


	protected boolean matchesInternal(T objectToCompare, Object unwantedObject) {
		return ObjectUtils.equals(objectToCompare, unwantedObject);
	}


	public void describeTo(Description description) {
		description.appendText("contained in ").appendValue(unwantedObjects);

	}

}
